<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-item label="设备编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-select  name="getequipmentList" placeholder="请选择设备编码"  v-decorator="['equipmentCode', validatorRules.equipmentCode]">
                <a-select-option v-for="(item,index) in devList"
                                 :value="item.code"
                                  @click="getOne(item)">
                {{item.code}}</a-select-option>
                </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="设备名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['equipmentName', validatorRules.equipmentName]" placeholder="请输入设备名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="设备型号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['equipmentMode', validatorRules.equipmentMode]" placeholder="请输入设备型号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="设备运行状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['equipmentStatus', validatorRules.equipmentStatus]" :trigger-change="true" dictCode="equipment_status" placeholder="请选择设备运行状态"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="操作人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['actionPerson', validatorRules.actionPerson]" placeholder="请输入操作人"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="开机时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择开机时间" v-decorator="['turnOnTime', validatorRules.turnOnTime]" :trigger-change="true" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="关机时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择关机时间" v-decorator="['turnOffTime', validatorRules.turnOffTime]" :trigger-change="true" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="保养日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择保养日期" v-decorator="['maintenanceDate', validatorRules.maintenanceDate]" :trigger-change="true" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="使用前是否核对" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['preStatus', validatorRules.preStatus]" :trigger-change="true" dictCode="pre_status" placeholder="请选择使用前状态"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="是否正常润滑" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['isOk', validatorRules.isOk]" :trigger-change="true" dictCode="is_ok" placeholder="请选择是否正常润滑"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="操作流程是否违规" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['isRules', validatorRules.isRules]" :trigger-change="true" dictCode="is_rules" placeholder="请选择操作流程是否违规"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="使用后是否清洁" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['isClear', validatorRules.isClear]" :trigger-change="true" dictCode="is_clear" placeholder="请选择使用后是否清洁"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="设备其他保养项目" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-editor v-decorator="['otherProject',{trigger:'input'}]"/>
            </a-form-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import JDate from '@/components/jeecg/JDate'  
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  import JEditor from '@/components/jeecg/JEditor'
  import {filterDictTextByCache} from "@comp/dict/JDictSelectUtil";

  export default {
    name: 'DevEquipmentMaintenanceForm',
    components: {
      JFormContainer,
      JDate,
      JDictSelectTag,
      JEditor,
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        equipmentName:'',
        equipmentMode:'',
        equipmentStatus:'',
        devList:[],
        equipmentCode:'',
        form: this.$form.createForm(this),
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          equipmentCode: {
            rules: [
              { required: true, message: '请输入设备编码!'},
            ]
          },
          equipmentName: {
            rules: [
              { required: true, message: '请输入设备名称!'},
            ]
          },
          equipmentMode: {
            rules: [
              { required: true, message: '请输入设备型号!'},
            ]
          },
          equipmentStatus: {
            rules: [
              { required: true, message: '请输入设备运行状态!'},
            ]
          },
          actionPerson: {
            rules: [
              { required: true, message: '请输入操作人!'},
            ]
          },
          turnOnTime: {
            rules: [
              { required: true, message: '请输入开机时间!'},
            ]
          },
          turnOffTime: {
            rules: [
              { required: true, message: '请输入关机时间!'},
            ]
          },
          maintenanceDate: {
            rules: [
              { required: true, message: '请输入保养日期!'},
            ]
          },
          preStatus: {
            rules: [
              { required: true, message: '请输入使用前状态!'},
            ]
          },
          isOk: {
            rules: [
              { required: true, message: '请输入是否正常润滑!'},
            ]
          },
          isRules: {
            rules: [
              { required: true, message: '请输入操作流程是否违规!'},
            ]
          },
          isClear: {
            rules: [
              { required: true, message: '请输入使用后是否清洁!'},
            ]
          },
        },
        url: {
          add: "/dev/devEquipmentMaintenance/add",
          edit: "/dev/devEquipmentMaintenance/edit",
          queryById: "/dev/devEquipmentMaintenance/queryById"
        },
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
      this.getequipmentList();

    },
    methods: {

      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'equipmentCode','equipmentName','equipmentMode','equipmentStatus','actionPerson','turnOnTime','turnOffTime','maintenanceDate','preStatus','isOk','isRules','isClear','otherProject'))
        })
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          });
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'equipmentCode','equipmentName','equipmentMode','equipmentStatus','actionPerson','turnOnTime','turnOffTime','maintenanceDate','preStatus','isOk','isRules','isClear','otherProject'))
      },
      //得到全部设备
      getequipmentList(){

        getAction("/dev/devEquipment/devs").then((res) => {

          var result = res.result;

          this.devList = result;

        } )
      },
      getOne(item){
        console.log("事件触发");
        this.form.setFieldsValue(
          {
            equipmentCode:item.code,
            equipmentName: item.name,
            equipmentMode: item.mode,
            equipmentStatus: item.status,
          }
        )
      },




    },

  }
</script>